package edu.ycxy.poetry.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.DjInfo;
import edu.ycxy.poetry.entity.DjList;
import edu.ycxy.poetry.mapper.DjInfoMapper;
import edu.ycxy.poetry.mapper.DjListMapper;
import edu.ycxy.poetry.service.IDjListService;
import edu.ycxy.poetry.vo.AddDjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/14
 * @Description
 */
@Service
public class DjListServiceImpl extends ServiceImpl<DjListMapper, DjList> implements IDjListService {

    @Autowired
    private DjListMapper djListMapper;
    @Autowired
    private DjInfoMapper djInfoMapper;

    //查询所有的古籍内容
    @Override
    public PageInfo<DjList> selectListByDjList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DjList> djLists = djListMapper.selectListByDjList();
        PageInfo pageInfo = new PageInfo(djLists);
        return pageInfo;
    }

    //根据古籍的id查询古籍的内容
    @Override
    public DjList selectOneById(Integer id) {
        return djListMapper.selectOneById(id);
    }

    @Override
    public List<DjList> selectListByType(String type) {
        return djListMapper.selectListByType(type);
    }

    @Override
    public String addDj(AddDjVo addDjVo) {
        // 根据古籍名判断古籍是否存在
        DjList byTitle = djListMapper.findByTitle(addDjVo.getTitle());
        DjInfo byTitle1 = djInfoMapper.findByTitle(addDjVo.getDjTitle());
        // 当不为空，说明古籍名已存在
        if (byTitle != null && byTitle1 == null){
            djInfoMapper.addDjInfo(addDjVo);
            return "ok";
        }else if (byTitle == null && byTitle1 == null){
            djListMapper.addDjInfo(addDjVo);
            djInfoMapper.addDjInfo(addDjVo);
            return "ok";
        }else {
            System.out.println("该古籍已存在！！");
            return "fail";
        }

    }


}
